import {
  LocationList,
  locationAdd,
  locationEdit,
  locationDetails,
  disabledLocationInfo,
  enableLocationInfo,
} from '@/services/Location';
import { memberExpression } from '@babel/types';
import Prompt from '@/components/Prompt';

export default {
  namespace: 'Location',

  state: {
    LocationList: {},
    locationDetails: {},
  },

  effects: {
    //管理：
    *LocationList({ payload, callback }, { call, put }) {
      const response = yield call(LocationList, payload);
      if (response.code === 0) {
        // const { list, pageSize, total, pageNum } = response.data;
        const list = response.data;
        yield put({
          type: 'show',
          payload: {
            LocationList: {
              pagination: {
                // current: pageNum,
                // pageSize,
                // total,
              },
              // pagination: 'false',
              list,
            },
          },
        });
        callback && callback(list);
      }
    },
    *LocationOperate({ payload, callback }, { call }) {
      const response = yield call(payload.id ? locationEdit : locationAdd, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        Prompt({ content: message });
        if (callback) callback(response.data);
      }
    },
    *locationDetails({ payload, callback }, { call, put }) {
      const response = yield call(locationDetails, payload);
      if (response.code === 0) {
        yield put({
          type: 'detail',
          payload: {
            locationDetails: { [payload.id]: response.data },
          },
        });
        callback && callback(response.data);
      }
    },
    *ableLocationOperate({ payload, callback }, { call }) {
      const response = yield call(
        payload.type ? enableLocationInfo : disabledLocationInfo,
        payload
      );
      if (response.code === 0) {
        Prompt({ content: response.message });
        callback(response);
      }
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        LocationList: payload.LocationList,
      };
    },
    detail(state, { payload }) {
      return {
        ...state,
        locationDetails: { ...state.locationDetails, ...payload.locationDetails },
      };
    },
  },
};
